<?php
/**
 * User: lishu
 * Date: 12/7/2017 AD
 * Time: 11:36
 */

namespace App\Http\Controllers\MobileApi;

use Storage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Mysql\outstanding_translation;

class OutstandingTranslationController extends Controller
{
	use GlobalData;
	const ROUTE_PREFIX = '/api/*';

	protected $request;

	/**
	 * 创建新的OutstandingTranslation控制器实例
	 * @param \Illuminate\Http\Request $request
	 */
	public function __construct(Request $request)
	{
		$this->request = $request;
		$this->error = [];
		$this->curTime = time();

		if(config('app.debug')) {
			error_reporting(E_ALL | E_STRICT);
		}
	}

	public function get()
    {
        $p = $_REQUEST;
		//test case:api/OutstandingTranslation?type=abc
		if (!(isset($p['pageNo']))){
            $data = array(
                'code' => 5,
                'msg' => '参数不全',
            );
            exit(json_encode($data));			
		}

		//test case:api/OutstandingTranslation?pageNo=2a
		if (!(ctype_digit($p['pageNo']))){
            $data = array(
                'code' => 5,
                'msg' => 'pageNo的参数不是整数',
            );
            exit(json_encode($data));			
		}

        $pageNo = $p['pageNo'];
		$taskCountPerPage=5;
		$taskStartIndex=$pageNo*$taskCountPerPage;
		
		//test case:api/OutstandingTranslation?pageNo=0
		$outstandingTranslationList = outstanding_translation::get_best($taskStartIndex, $taskCountPerPage+1);
		$polishedbestTranslationList = outstanding_translation::get_polishedbest($taskStartIndex, $taskCountPerPage+1);
		
		$outstandingCount = count($outstandingTranslationList);
		$polishedbestCount = count($polishedbestTranslationList);
		
		if($outstandingCount==0 && $polishedbestCount==0){
			$returnData = array(
				'code' => 200,
				'msg' => '亲，暂无数据',
				'outstandingTranslationListEmptyUrl' => config('applqyw.discuz_server_url').'template/comiis_nby/img/lqyw_jing_qing_qi_dai.png',
			);
			exit(json_encode($returnData));
		}

		$hasNextPage=false;
		if ($outstandingCount==($taskCountPerPage+1)){
			$hasNextPage=true;
			unset($outstandingTranslationList[$taskCountPerPage]);
			$outstandingCount=$taskCountPerPage;
		}
		if ($polishedbestCount==($taskCountPerPage+1)){
			$hasNextPage=true;
			unset($polishedbestTranslationList[$taskCountPerPage]);
			$polishedbestCount=$taskCountPerPage;
		}
				
		/*testing code
		$polishedbestTranslationList[0]= (object)[];
		$polishedbestTranslationList[0]->commentendtime=1505278800; //set as the commentendtime as the 2nd in outstandingTranslationList
		$polishedbestTranslationList[0]->polished=1;
		$polishedbestTranslationList[0]->authorid=2;
		$polishedbestTranslationList[0]->body="我是外校润色后的译文第一句。[//]第二句在这里。";
		*/
		
		$outstandingTranslationList = array_merge($outstandingTranslationList,$polishedbestTranslationList);
		usort($outstandingTranslationList, array('self', 'mycmp'));
		
		foreach($outstandingTranslationList as $translation){
			$translation->authorAvatarUrl=avatarUrl($translation->{'authorid'});
			$tmp_body = $translation->body;
			$tmp_body =str_replace("[//]"," ",strip_tags($tmp_body));
			$translation->body = messagecutstr($tmp_body);			
			unset($translation->commentendtime);
		}
		
		$returnData = array(
			'code' => 200,
			'msg' => '亲，优秀译文数据返回成功',
			'hasNextPage' => $hasNextPage,
			'outstandingTranslationList' => $outstandingTranslationList,
		);
		return(json_encode($returnData));

    }
	
	public static function mycmp($a,$b){
		if ($a->commentendtime > $b->commentendtime) {
			return -1;
		}
		if ($a->commentendtime < $b->commentendtime) {
			return 1;
		}
		
		if ($a->polished == $b->polished) {
			return 0;
		}
		return ($a->polished > $b->polished) ? -1 : 1;	
	}

}

